package com.ruoyi.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.IdVo;
import com.ruoyi.common.core.domain.PermVo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.user.domain.TbPerm;
import com.ruoyi.user.service.TbPermService;
import com.ruoyi.user.mapper.TbPermMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class TbPermServiceImpl extends ServiceImpl<TbPermMapper, TbPerm>
        implements TbPermService {

    @Autowired
    private TbPermMapper tbPermMapper;

    @Override
    public R addPerm(PermVo permVo) {

        TbPerm tbPerm = new TbPerm();
        BeanUtils.copyProperties(permVo, tbPerm);
        tbPerm.setUpdateBy("admin");
        tbPerm.setCreateBy("admin");
        int insert = tbPermMapper.insert(tbPerm);

        return insert > 0 ? R.ok("添加成功") : R.fail("添加失败");
    }

    @Override
    public R deletePerm(IdVo idVo) {

        int delete = tbPermMapper.deleteById(idVo.getId());

        return delete > 0 ? R.ok("删除成功") : R.fail("删除失败");
    }

    @Override
    public AjaxResult selectPerm() {
        List<TbPerm> tbPerms = tbPermMapper.selectList(null);

        return AjaxResult.success("查询可能为空",tbPerms);
    }

    @Override
    public R updatePerm(PermVo permVo) {

        TbPerm tbPerm = new TbPerm();
        BeanUtils.copyProperties(permVo, tbPerm);
        tbPerm.setUpdateBy("admin");
        tbPerm.setUpdateTime(null);

        int update = tbPermMapper.updateById(tbPerm);
        return update > 0 ? R.ok("修改成功") : R.fail("修改失败");
    }
}




